<template>
	<view class="user-renew" v-if="tabs.length">
		<view class="user-renew-tabs">
			<view :class="{active: tabsIndex==index}" v-for="(data,index) in tabs" :key="index" @click="change(index)">{{data.name}}</view>
		</view>
		<view class="user-renew-content">
			<view class="user-renew-tabs-1" v-if="tabsIndex==0">
				<image 
					:src="tabs[tabsIndex].image" 
					mode="widthFix">
				</image>
			</view>
			<view class="user-renew-tabs-2" v-if="tabsIndex==1">
				<image 
					:src="tabs[tabsIndex].image" 
					mode="widthFix">
				</image>
			</view>
		</view>
		<view class="user-renew-btns">
			<view class="user-renew-tabs-1-btns" v-if="tabsIndex==0">
				<view class="applocation" @click="applocation">意向申请</view>
				<view class="renew" :class="{isPay: isPay}" @click="$u.throttle(submit, 2000)">
					<u-loading mode="flower" v-if="isPay"></u-loading>{{isPay?'正在支付':'立即续费'}} <text>{{tabs[tabsIndex].renew}}元/年</text>
				</view>
			</view>
			<view class="user-renew-tabs-1-btns" v-if="tabsIndex==1">
				<view class="applocation" @click="applocation">意向申请</view>
				<view class="renew" :class="{isPay: isPay}" @click="$u.throttle(submit, 2000)">
					<u-loading mode="flower" v-if="isPay"></u-loading>{{isPay?'正在支付':'立即续费'}} <text>{{tabs[tabsIndex].renew}}元/年</text>
				</view>
			</view>
		</view>
		<uni-popup type="bottom" ref="applocation">
			<view class="applocation-pop">
				<view class="applocation-close" @click="closeApplocation"><image src="https://jiajudashi.oss-cn-shenzhen.aliyuncs.com/images/shop/icons/renew/close.png"></image></view>
				<view class="applocation-tit">意向申请</view>
				<view class="qrcode">
					<image 
						show-menu-by-longpress 
						src="https://jiajudashi.oss-cn-shenzhen.aliyuncs.com/images/shop/icons/renew/code.png"
						@longpress="savePhoto('https://jiajudashi.oss-cn-shenzhen.aliyuncs.com/images/shop/icons/renew/code.png')">
					</image>
				</view>
				<view class="applocation-tips">扫码加微信了解更多~<text>长按即可保存</text></view>
				<view class="applocation-phone" @click="makePhone">手机号：19802009157<text>联系TA</text></view>
			</view>
		</uni-popup>
		
		<!--支付方式-->
		<uni-popup type="bottom" ref="payWay">
			<view class="payWay-box">
				<view class="payWay-tit">
					请选择支付方式 
					<text>
						合计：
						<text>
							￥
							<block v-if="tabsIndex==0">{{tabs[tabsIndex].renew}}</block>
							<block v-if="tabsIndex==1">{{tabs[tabsIndex].renew}}</block>
						</text>
					</text>
				</view>
				<view class="payWay-list">
					<view class="payWay-item" @click="payAgain(1)">
						<image src="https://master.jiaju01.com/web/static/icons/ico_wechat.png"></image>
						<view class="pay-name">微信</view>
					</view>
					<view class="payWay-item" @click="payAgain(2)">
						<image src="https://master.jiaju01.com/web/static/icons/ico_alipay.png"></image>
						<view class="pay-name">支付宝</view>
					</view>
				</view>
			</view>
		</uni-popup>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabsIndex: 0,
				tabs: [],
				isPay: false
			};
		},
		onLoad() {
			this.init()
		},
		methods: {
			async init(){
				const { datas } = await this.$u.api.StoreTypeList()
				this.tabs = datas
			},
			change(index){
				this.tabsIndex = index
			},
			applocation(){
				this.$refs.applocation.open()
			},
			makePhone(){
				uni.makePhoneCall({
					phoneNumber: '19802009157'
				})
			},
			closeApplocation(){
				this.$refs.applocation.close()
			},
			submit(){
				// #ifdef H5
				uni.showModal({
					title: '温馨提示',
					content: '暂不支持网页版续费，请移步《家具大师总部商城》小程序或《家具大师》APP中续费，感谢您的谅解！',
					showCancel: false
				})
				return false
				// #endif
				// #ifdef MP-WEIXIN
				if(this.isPay){
					uni.showToast({
						icon: 'none',
						title: '正在发起支付，请勿重复点击'
					})
					return false
				}
				this.isPay = true
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						let data = {
							id: this.tabsIndex==0?4:5,
							pay_id: 1,
							pay_client: 8,
							code: loginRes.code
						}
						this.$u.api.GoStorePay(data).then(res => {
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.datas.timeStamp,
								nonceStr: res.datas.nonceStr,
								package: res.datas.package,
								signType: res.datas.signType,
								paySign: res.datas.paySign,
								success: (successRes) => {
									uni.showToast({
										icon: 'none',
										title: '支付成功，恭喜您续费成功',
										duration: 1500
									})
									setTimeout(() => {
										uni.switchTab({
											url: '/pages/my/my'
										})
									},1500)
									this.isPay = false
								},
								fail: (err) => {
									let msg = err.errMsg
									if(msg.indexOf('cancel')!=-1){
										uni.showToast({
											icon: 'none',
											title: '取消支付',
											duration: 1500
										})
									}else{
										uni.showToast({
											icon: 'none',
											title: '支付失败',
											duration: 1500
										})
									}
									this.isPay = false
								}
							});
						})
					}
				})
				// #endif
				// #ifdef APP-PLUS
				this.$refs.payWay.open()
				// #endif
			},
			payAgain(type){
				this.$u.throttle(this.submitPay(type),2000)
			},
			submitPay(type){
				uni.showLoading({
					title: '支付中'
				})
				let id
				if(this.tabsIndex==0){
					id = 4
				}else if(this.tabsIndex==1){
					id = 5
				}
				let data = {
					id: id,
					pay_id: type,
					pay_client: 2
				}
				this.$u.api.GoStorePay(data).then(res => {
					if(type==1){
						uni.requestPayment({
							provider: 'wxpay',
							orderInfo: {
								appid: res.datas.appid,
								noncestr: res.datas.noncestr,
								package: res.datas.packages,
								partnerid: res.datas.partnerid,
								prepayid: res.datas.prepayid,
								timestamp: res.datas.timestamp,
								sign: res.datas.sign
							},
							success: (successRes) => {
								uni.showToast({
									icon: 'none',
									title: '支付成功，恭喜您续费成功',
									duration: 1500
								})
								setTimeout(() => {
									uni.switchTab({
										url: '/pages/my/my'
									})
								},1500)
							},
							fail: (err) => {
								let msg = err.errMsg
								if(msg.indexOf('cancel')!=-1){
									uni.showToast({
										icon: 'none',
										title: '取消支付',
										duration: 1500
									})
								}else{
									uni.showToast({
										icon: 'none',
										title: '支付未完成',
										duration: 1500
									})
								}
							}
						});
					}else{
						uni.requestPayment({
							provider: 'alipay',
							orderInfo: res.datas,
							success: (alipaySuccess) => {
								uni.showToast({
									icon: 'none',
									title: '支付成功，恭喜您续费成功',
									duration: 1500
								})
								setTimeout(() => {
									uni.switchTab({
										url: '/pages/my/my'
									})
								},1500)
							},
							fail: (alipayFail) => {
								uni.showToast({
									icon: 'none',
									title: '支付已取消',
									duration: 1500
								})
							}
						})
					}
				})
			},
			savePhoto(url) {
				// #ifdef APP-PLUS
				uni.downloadFile({
					url: url,
					success: (res) => {
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success: () => {
								uni.showToast({
									icon: 'none',
									title: "保存成功",
								})
							},
							fail: () => {
								uni.showToast({
									icon: 'none',
									title: "保存失败",
								})
							}
						})
					}
				})
				// #endif
			}
		}
	}
</script>

<style lang="scss">
	.user-renew{
		padding-top: 98rpx;
		padding-bottom: 120rpx;
		.user-renew-tabs{
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			height: 98rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			z-index: 10;
			background-color: #FFF;
			view{
				border: solid 2rpx #F1F1F1;
				height: 60rpx;
				line-height: 56rpx;
				font-size: 28rpx;
				width: 180rpx;
				text-align: center;
				&:first-child{
					border-radius: 8rpx 0px 0px 8rpx;
				}
				&:last-child{
					border-radius: 0px 8rpx 8rpx 0px;
					border-left: none;
				}
				&.active{
					background-color: #141414;
					color: #FFF;
				}
			}
		}
		.user-renew-content{
			image{
				width: 100%;
			}
		}
		.user-renew-btns{
			position: fixed;
			z-index: 10;
			bottom: 0;
			left: 0;
			right: 0;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 120rpx;
			background-color: #2B2A28;
			.user-renew-tabs-1-btns{
				height: 80rpx;
				display: flex;
			}
			.applocation{
				width: 240rpx;
				height: 80rpx;
				text-align: center;
				line-height: 80rpx;
				background-color: #FDE7C6;
				font-size: 30rpx;
				color: #000;
			}
			.renew{
				width: 380rpx;
				height: 80rpx;
				line-height: 80rpx;
				background: linear-gradient(to bottom, #FDE7C6, #CCAB84);
				border-radius: 4rpx;
				margin-left: 20rpx;
				text-align: center;
				font-size: 30rpx;
				text{
					font-size: 24rpx;
					margin-left: 20rpx;
				}
				&.isPay{
					opacity: .5;
					color: #FFF;
				}
			}
		}
		.applocation-pop{
			background-color: #FFF;
			border-radius: 20rpx 20rpx 0px 0px;
			padding: 30rpx 30rpx 60rpx;
			.applocation-close{
				position: absolute;
				right: 30rpx;
				top: 30rpx;
				line-height: 40rpx;
				image{
					width: 21rpx;
					height: 21rpx;
					vertical-align: middle;
				}
			}
			.applocation-tit{
				text-align: center;
				font-size: 36rpx;
				font-weight: bold;
				line-height: 48rpx;
			}
			.qrcode{
				text-align: center;
				padding: 30rpx 0;
				image{
					width: 205rpx;
					height: 205rpx;
					border: solid 1px #EEE;
				}
			}
			.applocation-tips{
				text-align: center;
				font-size: 28rpx;
				color: #656565;
				text{
					margin-left: 10rpx;
					font-size: 24rpx;
					color: #999;
				}
			}
			.applocation-phone{
				text-align: center;
				margin-top: 30rpx;
				text{
					display: inline-block;
					border: solid 2rpx #E6E6E6;
					height: 40rpx;
					line-height: 36rpx;
					margin-left: 20rpx;
					font-size: 24rpx;
					padding: 0 20rpx;
					border-radius: 8rpx;
				}
			}
		}
		.payWay-box {
			background-color: #FFF;
			padding: 20rpx;
			.payWay-tit{
				display: flex;
				justify-content: space-between;
				margin-bottom: 40rpx;
				font-size: 32rpx;
				padding:0 40rpx 30rpx;
				border-bottom: solid 1px #EEE;
				text{
					text{
						color: #FF0000;
					}
				}
			}
			.payWay-list{
				display: flex;
				align-items: center;
				.payWay-item{
					flex: 1;
					text-align: center;
					image{
						width: 100rpx;
						height: 100rpx;
					}
					view{
						font-size: 30rpx;
						color: #888;
						margin-top: 10rpx;
					}
				}
			}
		}
	}
</style>
